/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sUtils;

import java.util.*;
import javax.microedition.lcdui.*;
/**
 *
 * @author thomas.s
 */
public class Keypad {

    Vector buttons = new Vector(50,2);

    class Button
    {
        public float x,y,size;
        public int ix, iy, iw, ih, icaps=0;
        public String text,s;
        Image bg=null;
        Keypad pad=null;

        public Button( String _text, float col, float row, float _size )
        {
            text=_text; x=col; y=row; size=_size; iw=ih=ix=iy=0;
        }

        public void update( Graphics g, int caps )
        {
            if (icaps!=caps || iw==0)
            {
            int w = g.getClipWidth();
            int h = g.getClipHeight();
            ix = (int)(x*(float)w/10.0f);
            iy = (int)((y+7)*(float)h/12.0f);
            iw = (int)((w*(float)size)/10);
            ih = h/12;
            icaps = caps;

            if (text.length()!=2) s = text; else s = "" + text.charAt(caps);

            
            }
        }


        public void draw( Graphics g, int caps )
        {
            update(g, caps);
            g.setColor( 0x303080 );
            g.fillRoundRect(ix+2, iy+2, iw-4, ih-4, ih/4, ih/4);
            g.setColor(0xf0f0ff);
            Font f = g.getFont();
            g.drawString(s, ix+(iw-f.stringWidth(s))/2, iy+(ih-f.getHeight())/2, 0);
           // g.drawImage(bg, ix, iy, 0);
        }

        public boolean hit( int x, int y )
        {
            if (x>=ix+2 && y>=iy+2 && x<(ix+iw-2) && y<(iy+ih-2))
            {   // down = true;
                 return true;
            }
            return false;
        }
    }

    public Keypad()
    {
        buttons.addElement( new Button("!1",0,0,1));
        buttons.addElement( new Button("\"2",1,0,1));
        buttons.addElement( new Button("@3",2,0,1));
        buttons.addElement( new Button("$4",3,0,1));
        buttons.addElement( new Button("%5",4,0,1));
        buttons.addElement( new Button("&6",5,0,1));
        buttons.addElement( new Button("/7",6,0,1));
        buttons.addElement( new Button("(8",7,0,1));
        buttons.addElement( new Button(")9",8,0,1));
        buttons.addElement( new Button("=0",9,0,1));

        buttons.addElement( new Button("Qq",0,1,1));
        buttons.addElement( new Button("Ww",1,1,1));
        buttons.addElement( new Button("Ee",2,1,1));
        buttons.addElement( new Button("Rr",3,1,1));
        buttons.addElement( new Button("Tt",4,1,1));
        buttons.addElement( new Button("Zz",5,1,1));
        buttons.addElement( new Button("Uu",6,1,1));
        buttons.addElement( new Button("Ii",7,1,1));
        buttons.addElement( new Button("Oo",8,1,1));
        buttons.addElement( new Button("Pp",9,1,1));

        buttons.addElement( new Button("Aa",0.5f,2,1));
        buttons.addElement( new Button("Ss",1.5f,2,1));
        buttons.addElement( new Button("Dd",2.5f,2,1));
        buttons.addElement( new Button("Ff",3.5f,2,1));
        buttons.addElement( new Button("Gg",4.5f,2,1));
        buttons.addElement( new Button("Hh",5.5f,2,1));
        buttons.addElement( new Button("Jj",6.5f,2,1));
        buttons.addElement( new Button("Kk",7.5f,2,1));
        buttons.addElement( new Button("Ll",8.5f,2,1));

        buttons.addElement( new Button("><",0,3,1));
        buttons.addElement( new Button("Yy",1,3,1));
        buttons.addElement( new Button("Xx",2,3,1));
        buttons.addElement( new Button("Cc",3,3,1));
        buttons.addElement( new Button("Vv",4,3,1));
        buttons.addElement( new Button("Bb",5,3,1));
        buttons.addElement( new Button("Nn",6,3,1));
        buttons.addElement( new Button("Mm",7,3,1));
        buttons.addElement( new Button(";,",8,3,1));
        buttons.addElement( new Button(":.",9,3,1));

        buttons.addElement( new Button("Cap",0,4,2));
        buttons.addElement( new Button("Space",2,4,4));
        buttons.addElement( new Button("<",6,4,1));
        buttons.addElement( new Button(">",7,4,1));
        buttons.addElement( new Button("Del",8,4,2));
    }

    public void paint(Graphics g, int caps)
    {
        Font f = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE);
        g.setFont( f );
        g.setColor(0xf0f0f0);
        for ( int j=0; j<buttons.size(); j++ )
        {
            Button b = (Button)buttons.elementAt(j);
            b.draw(g,caps);
        }
    }

    public String hitTest( int x, int y )
    {
        for ( int j=0; j<buttons.size(); j++ )
        {
            Button b = (Button)buttons.elementAt(j);
            if (b.hit(x,y)) return b.text;
        }
        return null;
    }
}
